from django.shortcuts import render
from django.http import *

# Create your views here.
from view_study.models import *

# def error_methord(request):
#     print("视图函数报错!!")
#     raise ConnectionError



def post1(request):

    return render(request, "form.html")


def check_login(request):
    g_dict = {}
    g_dict['uname'] = request.POST.get('uname')
    g_dict['upwd'] = request.POST.get('upwd')
    g_dict['ugender'] = request.POST.get('ugender')
    g_dict['uhobby'] = request.POST.get('uhobby')
    # todo:获取多个，多选
    g_dict['uhobby'] = request.POST.getlist('uhobby')
    print(g_dict)
    # return JsonResponse(g_dict)
    # return HttpResponse("ok")
    return render(request, "post_info.html", context=g_dict)


def index(request):
    student_list = Student.objects.all()
    return render(request, "index.html", context={"student_list": student_list})


# 自定义错误视图函数
def not_found(request):
    return render(request, "404.html")


def add_name(request, name):
    # s1 = Student()
    # s1.name = name
    # s1.age = 18
    # s1.save()
    my = Student.objects.create(name=name, age=18)
    my.save()

    return HttpResponse("ok")


def add_object(request, name, age):
    # s2 = Student()
    # s2.name = name
    # s2.age = age

    h2 = Student.objects.create(name=name, age=age)
    h2.save()
    return HttpResponse("ok")


# 明文
def get_info(request):
    # get:根据键获取值,如果有多个键则获取最后一个值,不存在就返回一个none值,也可以设置默认值
    get_dict = {}
    get_dict["a"] = request.GET.get("a")
    print(get_dict)
    # http://127.0.0.1:8000/get_info/?a=1&a=2
    # 获取到a的全部赋值,以一个列表储存相应的值
    get_dict["a"] = request.GET.getlist("a")
    print(get_dict)  # {'a': ['1', '2']}
    return HttpResponse("ok")


def post(request):
    post_list = Post.objects.all()
    return render(request, "post_list.html", context={"post_list": post_list})


# 常见的查询方法
def search(request):
    # 数据的查询
    # 1.contain 包含
    post_list = Post.objects.filter(name__contains="数")
    print(post_list)  # [<Post: 高数期末知识梳理>, <Post: 导数的高级应用>]

    # 2.startswith 以什么开头
    post_list = Post.objects.filter(name__startswith="定积分")
    print(post_list)  # [<Post: 定积分的高级计算>]

    # 3.endswith 以什么结尾
    post_list = Post.objects.filter(name__endswith="应用")
    print(post_list)  # [<Post: 导数的高级应用>]

    # 4.返回除了xxx之外的所有对象 exclude
    post_list = Post.objects.exclude(name__startswith="导数")
    print(post_list)

    # 5.order by 排序
    post_list = Post.objects.order_by("-author_name_id")
    print(post_list)

    # 6.gte 大于等于
    print(Post.objects.filter(id__gte=5))

    # 7.lte 小于等于
    print(Post.objects.filter(id__lte=5))

    # 8.get获取单条数据
    print(Post.objects.get(id=1))

    # 9.count() 返回当前查询的总条数
    print(Post.objects.order_by("author_name_id").count())

    # 10.exists()查询对象是否存在 返回布尔值
    print(Post.objects.filter(author_name_id=20).exists())  # False

    # 11. F对象 在对象中取出两个值进行比较时用到
    from django.db.models import F, Q, Max, Min
    # 打印id值等于分类id的文章
    print(Post.objects.filter(category_id=F("id")))

    # 12.Q对象 逻辑与,逻辑或, 逻辑非,对对象的某一个属性进行判断时用到
    # print(Post.objects.filter(Q(id=1)) & Q(author_name_id=1))
    # return HttpResponse("ok")

    # 13. 关联查询  如果是一对多,多对多,直接去连字段,不用管表名
    print(Post.objects.filter(name="python自然语言"))
    print(Post.objects.filter(author_name__author_name="久违"))
    # 这里面post中的tag字段是连到外面的,所以要找到关联的字段并且用"__"链接
    print(Post.objects.filter(tag__tag_name="java"))

    # 查询分类id为1的所有文章
    c1 = Category.objects.get(id=1)
    print(c1)
    # 方法一(正向查询)
    print(Post.objects.filter(category__category_name=c1))

    # 方法二   先查分类再查文章(少--多)  反向查询
    # post_list1 = c1.post_set.all()
    # print(post_list1)

    # 查询标签id为2的所有文章
    t1 = Tag.objects.get(id=2)
    post_list2 = t1.post_set.all()
    print(post_list2)

    # 查询作者id为1的所有文章
    a1 = Author.objects.get(id=1)
    post_list = a1.post_set.all()
    print(post_list)

    # 查询文章id为1的类名
    print(Post.objects.get(id=1).category.category_name)  # 推荐这样相当于访问了类的category_name
    print(Post.objects.get(id=1).category)  # 不推荐,这样相当于触发了__str__方法

    # 聚合查询 Max,Min,Sum,Avg,Count 需要提前导入
    print(Post.objects.aggregate(Max("id")))  # {'id__max': 11}
    print(Post.objects.aggregate(Min("id")))  # {'id__min': 1}

    return HttpResponse("ok")
